import json
import connDB
import 单个CDM as singleCDM
import copy

# 预加载模板内容
def load_template():
    with open('模板/cdm作业模板.json', 'r', encoding='utf8') as f:
        return json.load(f)

#定义一个函数生成单个CDM作业对象
def generateCDMJobObj(template_data,groupId,groupName,
            from_link_name, from_schema_name, from_table_name,connFrom,
            to_link_name, to_schema_name, to_table_name,connTo):
    job_name = f'{from_table_name}-{to_table_name}'
    if not singleCDM.check_table_exists(connFrom, from_schema_name, from_table_name):
        print(f"表{from_table_name}不存在")
        return None
    from_column_list =singleCDM.get_table_columns(connFrom, from_schema_name, from_table_name)
    to_column_list =from_column_list
    # 深拷贝模板数据，避免修改原始模板
    jsonobj = copy.deepcopy(template_data)

    jobObj=jsonobj['jobs'][0]
    toJobConfig = jobObj['to-config-values']['configs'][0]['inputs']
    toJobConfig[0]['value'] = to_schema_name
    toJobConfig[2]['value'] = to_table_name
    toJobConfig[3]['value'] = to_column_list
    fromJobConfig = jobObj['from-config-values']['configs'][0]['inputs']
    fromJobConfig[1]['value'] = from_schema_name
    fromJobConfig[2]['value'] = from_table_name
    fromJobConfig[4]['value'] = from_column_list
    jobObj['to-link-name'] = to_link_name
    jobObj['driver-config-values']['configs'][6]['inputs'][0]['value'] = groupId
    jobObj['driver-config-values']['configs'][6]['inputs'][1]['value'] = groupName
    jobObj['from-link-name'] = from_link_name
    jobObj['name'] = job_name
    return jobObj


def generateCDMJobFile(groupId,groupName,
                       from_link_name, from_schema_name,connFrom,
                       to_link_name, to_schema_name,connTo):
    # 预加载模板
    template_data = load_template()
    with open(f"模板/批量CDM作业模板.json", "r", encoding='utf8') as f2:
        with open("批量CDM表名.csv", "r", encoding='utf8') as csv:
            with open(f"cdm_json/批量CDM作业.json", "w", encoding='utf8') as out:
                jsobObj=json.load(f2)
                for line in csv:
                    line = line.strip()
                    if line:
                        from_table_name, to_table_name = line.split(",")
                        jobObj=generateCDMJobObj(
                                template_data,groupId,groupName,
                                from_link_name, from_schema_name, from_table_name,connFrom,
                                to_link_name, to_schema_name, to_table_name,connTo)
                        jsobObj['jobs'].append(jobObj)
                json.dump(jsobObj, out, indent=2, ensure_ascii=False)
                print(f"生成作业文件成功：批量CDM作业.json")


if __name__ == "__main__":
    groupId='13'
    groupName='dm_ynfd'

    from_link_name='dws_dm_prd'
    from_schema_name='dt_yunnan'

    to_link_name="yunnan"
    to_schema_name='ods_dtjt'


    #来源表连接
    connFrom = connDB.create_connection(**connDB.dm_prd_config)
    #目的表连接
    connTo = connDB.create_connection(**connDB.dm_prd_config)
    if connFrom :
        print("数据库连接成功")
        generateCDMJobFile(groupId,groupName, from_link_name, from_schema_name,connFrom,to_link_name, to_schema_name,connTo)
    else:
        print("数据库连接失败")







